package com.seal.tsms.base.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerView;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import java.util.Properties;

/**
 * spring mvc 4  零xml配置
 * 此类的作用类似于 xxx-servlet.xml
 * 继承WebMvcConfigurerAdapter类简化编写mvc配置
 * Created by seal.jing on 2015/12/20.
 */

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.seal.tsms", useDefaultFilters = false, includeFilters = { @ComponentScan.Filter(value = { Controller.class }) })
@PropertySource({"classpath:properties/view.properties"})
@EnableAspectJAutoProxy
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    protected Logger log= LoggerFactory.getLogger(getClass());
    {
        log.info(getClass().getName()+" 的对象初始化 .....");
    }
    @Autowired
    private Environment env;

    /**
     * json 解决方案
     * 用于支持RequestBody 和ResponseBody
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        MappingJackson2HttpMessageConverter bean=new MappingJackson2HttpMessageConverter();
        return bean;
    }

    /**
     * jsp 试图解决方案
     * order 属性用来解决多视图解决方案
     * @return
     */
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver(){
        InternalResourceViewResolver bean=new InternalResourceViewResolver();
        bean.setViewClass(JstlView.class);
        bean.setPrefix(env.getProperty("jspSettings.prefix"));
        bean.setSuffix(env.getProperty("jspSettions.suffix"));
        bean.setContentType("text/html;charset=UTF-8");
        bean.setOrder(1);
        return bean;
    }

    /**
     *
     *附件Multipart解决方案
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver bean=new CommonsMultipartResolver();
        bean.setDefaultEncoding("utf-8");
        bean.setMaxUploadSize(102400000);
        return bean;
    }

    /**
     * FreeMarker视图层解决方案
     * @return
     */
    @Bean
    public FreeMarkerViewResolver freeMarkerViewResolver(){
        FreeMarkerViewResolver bean = new FreeMarkerViewResolver();
        bean.setViewClass(FreeMarkerView.class);
        // bean.setPrefix(env.getProperty("freemarkerSettings.prefix"));
        bean.setRequestContextAttribute("request");

        bean.setSuffix(env.getProperty("freemarkerSettings.suffix"));
        bean.setContentType("text/html;charset=UTF-8");
        bean.setOrder(0);
        return bean;
    }

    /**
     * FreeMarker配置工厂
     * @return
     */
    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(){
        FreeMarkerConfigurer bean = new FreeMarkerConfigurer();
        bean.setTemplateLoaderPath(env.getProperty("freemarkerSettings.prefix"));
        bean.setDefaultEncoding("utf-8");

        Properties properties=new Properties();
        properties.setProperty("localized_lookup","false");
        properties.setProperty("template_update_delay",env.getProperty("freemarkerSettings.template_update_delay"));
        properties.setProperty("url_escaping_charset",env.getProperty("freemarkerSettings.url_escaping_charset"));
        properties.setProperty("locale",env.getProperty("freemarkerSettings.locale"));
        properties.setProperty("boolean_format",env.getProperty("freemarkerSettings.boolean_format"));
        properties.setProperty("datetime_format",env.getProperty("freemarkerSettings.datetime_format"));
        properties.setProperty("date_format",env.getProperty("freemarkerSettings.date_format"));
        properties.setProperty("time_format",env.getProperty("freemarkerSettings.time_format"));
        properties.setProperty("number_format",env.getProperty("freemarkerSettings.number_format"));
       // properties.setProperty("auto_import",env.getProperty("freemarkerSettings.auto_import"));
        //properties.setProperty("auto_include",env.getProperty("freemarkerSettings.auto_include"));
        properties.setProperty("template_exception_handler",env.getProperty("freemarkerSettings.template_exception_handler"));
        bean.setFreemarkerSettings(properties);

        return bean;
    }


}
